package com.meme.editor.gui.sprite2d;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.awt.event.FocusListener;

import java.util.Hashtable;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ListSelectionListener;

import org.apache.log4j.Logger;

import com.meme.app.AppRegistry;
import com.meme.editor.event.EntityChangedEvent;
import com.meme.editor.event.EntityObjectListener;
import com.meme.editor.gui.PropertyEditPanel;
import com.meme.editor.model.sprite2d.CompositedModule;
import com.meme.editor.model.sprite2d.Module;
import com.meme.editor.model.sprite2d.ModuleRef;
import com.meme.editor.model.sprite2d.Tiled2dLayerCell;
import com.meme.editor.model.sprite2d.Tiled2dPalette;
import com.meme.editor.model.sprite2d.Tiled2dBackground;
import com.meme.editor.model.sprite2d.Tiled2dLayer;
import com.meme.editor.EntityObject;
import com.meme.editor.renderer.Tiled2dBackgroundRenderer;



public class Tiled2dBackgroundView extends JPanel 
		implements EntityObjectListener, AncestorListener
			, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener//, ListSelectionListener
			, FocusListener
{
	static Logger logger = Logger.getLogger (Tiled2dBackgroundView.class);
	
	
	static final int MaxScale = 2;
	static final int MinScale = 1;
	static final int DefaultScale = 1;
	// for edit mode
	int mScale = DefaultScale;

	int mImageLeft; // with scale
	int mImageTop;
	
	int mImageWidth; // without scale
	int mImageHeight;
	
	PropertyEditPanel mCellPropertyEditPanel;
	Tiled2dBackground mTiled2dBackground;
	
	Hashtable<Tiled2dLayer, Tiled2dLayerStatus> mLayerStatus = new Hashtable<Tiled2dLayer, Tiled2dLayerStatus> ();
	Tiled2dGridSelectedRegion mTiled2dGridSelectedRegion;
	
	Tiled2dBackground mCopySourceBackground;
	
	Point mLastMousePos = new Point ();
	
	boolean mPaintGrid = true;
	
	boolean mPaintCellInfo = false;

	public Tiled2dBackgroundView (Tiled2dBackground tiled2dBackground)
	{
		mTiled2dBackground = tiled2dBackground;
		mTiled2dGridSelectedRegion = new Tiled2dGridSelectedRegion ();

		addAncestorListener (this);
		addMouseListener    (this);
		addMouseMotionListener (this);
		addMouseWheelListener (this);
		
		setFocusable (true);
		addFocusListener (this);
		addKeyListener (this);
	}
	
	public void setCellPropertyEditPanel (PropertyEditPanel modulePropertyPanel) 
	{
		mCellPropertyEditPanel = modulePropertyPanel;
	}
	
	public void setCopySourceBackground (Tiled2dBackground background)
	{
		mCopySourceBackground = background;
		
		repaint ();
	}

	@Override
	public void paintComponent (Graphics _g)
	{
		Graphics2D g = (Graphics2D)_g;
		updateSomeValues ( mScale, 0, 0);
		
		int viewWidth = getWidth ();
		int viewHeight = getHeight ();
		
		// clear bg
		Color defaultColor = g.getColor ();
		g.setColor ( SystemColor.window );
		g.fillRect(0, 0, viewWidth, viewHeight);
		

		
		// render Tiled2dBackground
		int originX = mImageLeft + viewWidth / 2;
		int originY = mImageTop + viewHeight / 2;
		int scaledW = mTiled2dBackground.getCellWidth () * mScale;
		int scaledH = mTiled2dBackground.getCellHeight() * mScale;


		Tiled2dBackgroundRenderer.renderTiled2dBackground(g, mTiled2dBackground, originX, originY, mScale);
		
		// paint grid
		if ( mPaintGrid )
		{
			int gridW = scaledW * mTiled2dBackground.getCellCols ();
			int gridH = scaledH * mTiled2dBackground.getCellRows ();
			
			for (int row = 0; row <= mTiled2dBackground.getCellRows (); ++ row)
			{
				int y = originY + row * scaledH;
				
				if (row % 10 == 0)
					g.setColor (SystemColor.textText);
				else
					g.setColor (SystemColor.textInactiveText);
	
				g.drawLine (originX, y, originX + gridW, y);
			}
			
			for (int col = 0; col <= mTiled2dBackground.getCellCols(); ++ col)
			{
				int x = originX + col * scaledW;
				
				if (col % 10 == 0)
					g.setColor (SystemColor.textText);
				else
					g.setColor (SystemColor.textInactiveText);
	
				g.drawLine (x, originY, x, originY + gridH);
			}
		}
		
		// paint selection box
		if ( isFocusOwner() )
		{
			updateGridSelectionRect ();
			Rectangle rect = new Rectangle (mGridSelectionRect);
			rect.translate(originX, originY);
			
			g.setColor(Color.green);
			
			g.drawRect(rect.x, rect.y, rect.width, rect.height);
			
			mMouseInGridSelectionRect = rect.contains(mLastMousePos);
			if (mCopySourceBackground == null && mMouseInGridSelectionRect )
			{
				Color c = new Color(0x7f0000ff, true);
				g.setColor (c);
				g.fillRect(rect.x, rect.y, rect.width, rect.height);
			}
		}

		
		// paint copy source background
		if (mCopySourceBackground != null)
		{
			int x = originX + (mLastMousePos.x - originX) / scaledW * scaledW;
			int y = originY + (mLastMousePos.y - originY) / scaledH * scaledH;
			Tiled2dBackgroundRenderer.renderTiled2dBackground(g, mCopySourceBackground, x, y, mScale);
			
			int rw = mCopySourceBackground.getCellCols () * scaledW;
			int rh = mCopySourceBackground.getCellRows () * scaledH;
			
			g.setColor(Color.red);
			
			g.drawRect(x, y, rw, rh);
		}
		else if (mPaintCellInfo)
		{
			Point lPoint = dp2lp (mLastMousePos);
			
			int gridW = mTiled2dBackground.getCellWidth ();
			int gridH = mTiled2dBackground.getCellHeight ();
			
			int gridCols = mTiled2dBackground.getCellCols ();
			int gridRows = mTiled2dBackground.getCellRows ();
			
			if (lPoint.x >= 0 && lPoint.y >= 0)
			{
				int cellX = lPoint.x / gridW;
				int cellY = lPoint.y / gridH;
				
				if (cellX < gridCols && cellY <= gridRows)
				{
					Vector <String>  infoStrings = new Vector <String> (20);
					infoStrings.add ( new String ("pos : ( " + lPoint.x + ", " + lPoint.y + ")") );
					infoStrings.add ( new String ("cell: ( " + cellY + ", " + cellX + ")") );
					
					for (int layerID = 0; layerID < mTiled2dBackground.getLayersCount(); ++ layerID)
					{
						Tiled2dLayer layer = mTiled2dBackground.getLayerByIndex (layerID);
						Tiled2dLayerStatus layerStatus = Tiled2dLayerListCellRenderer.getTiled2dLayerStatus (layer);
						if ( ! layerStatus.isVisible() )
							continue;
							
						Tiled2dLayerCell layerCell = layer.getLayerCellByPos(cellY, cellX);
						Module module = layerCell.getModule();
						boolean flipX = layerCell.isFlipX();
						boolean flipY = layerCell.isFlipY();
						
						
						infoStrings.add ( new String (" - layer #" + layerID + ": " + (module == null ? -1 : module.getIndex()) 
												+ (flipX ? " X" : "")
												+ (flipY ? " Y" : "")
												) );
					}
					
					drawInfoStrings (g, mLastMousePos.x, mLastMousePos.y, infoStrings, 0x0000ff, 0xffffdd, 0x0);
				}
			}
		}


		
		// paint rulers

	}
	
	protected void drawInfoStrings (Graphics g, int x, int y, Vector<String> infoStrings, int textColor, int bgColor, int borderColor)
	{
		FontMetrics fm = g.getFontMetrics();
		int fontHeight = fm.getHeight();
		int infoPageHeight = fontHeight * infoStrings.size();
		int infoPageWidth = 0;
		
		for (int strID = 0; strID < infoStrings.size(); ++ strID)
		{
			String str = infoStrings.elementAt(strID);
			
			int strW = str == null ? 0 : fm.stringWidth(str);
			
			if (strW > infoPageWidth)
				infoPageWidth = strW;
		}
		
		infoPageWidth += 6;
		infoPageHeight += 6;
		
		x -= infoPageWidth;
		y -= infoPageHeight;
		
		Color oldColor = g.getColor();
		g.setColor(new Color (bgColor));
		g.fillRect(x, y, infoPageWidth, infoPageHeight);
		g.setColor(new Color (borderColor));
		g.drawRect(x, y, infoPageWidth, infoPageHeight);
		g.setColor(new Color (textColor));
		
		x += 3;
		y += 3 + fm.getAscent();
		
		for (int strID = 0; strID < infoStrings.size(); ++ strID)
		{
			String str = infoStrings.elementAt(strID);
			
			g.drawString(str, x, y);
			
			y += fontHeight;
		}
		
		g.setColor (oldColor);
	}
	
	protected void updateSomeValues (int newScale, int dx, int dy)
	{
		float ratioX;
		float ratioY;
		
		if ( mImageWidth != 0 )
			ratioX = (float) mImageLeft / (float) ( mImageWidth * mScale );
		else
			ratioX = - 0.5f;
		
		if ( mImageHeight != 0 )
			ratioY = (float) mImageTop / (float) ( mImageHeight * mScale );
		else
			ratioY = - 0.5f;
		
		mScale = newScale;
		
		if ( mTiled2dBackground != null )
		{
			mImageWidth  = mTiled2dBackground.getCellWidth() * mTiled2dBackground.getCellCols();
			mImageHeight = mTiled2dBackground.getCellHeight() * mTiled2dBackground.getCellRows();

			mImageLeft = (int) ( ratioX * (mImageWidth  * mScale) );
			mImageTop  = (int) ( ratioY * (mImageHeight * mScale) );
		}
		else
		{
			mImageLeft = 0;
			mImageTop  = 0;
			mImageWidth  = 0;
			mImageHeight = 0;
			
			return;
		}
		
		mImageLeft += dx;
		mImageTop  += dy;
		
		//
		if ( mImageLeft > 0 )
			mImageLeft = 0;
		if ( mImageTop > 0 )
			mImageTop = 0;
		int imgRight  = mImageLeft + (mImageWidth  * mScale);
		int imgBottom = mImageTop  + (mImageHeight * mScale);
		if ( imgRight < 0 )
			mImageLeft -= imgRight;
		if ( imgBottom < 0 )
			mImageTop -= imgBottom;

	}
	
	Rectangle mGridSelectionRect = new Rectangle ();
	boolean mMouseInGridSelectionRect = false;
	void updateGridSelectionRect ()
	{
		int originX = 0;
		int originY = 0;
		int scaledW = mTiled2dBackground.getCellWidth () * mScale;
		int scaledH = mTiled2dBackground.getCellHeight() * mScale;

		
		int rx = originX + mTiled2dGridSelectedRegion.mLeft * scaledW;
		int ry = originY + mTiled2dGridSelectedRegion.mTop  * scaledH;
		
		int rw = mTiled2dGridSelectedRegion.getCols () * scaledW;
		int rh = mTiled2dGridSelectedRegion.getRows () * scaledH;
		
		mGridSelectionRect.setBounds (rx, ry, rw, rh);
	}
	
	protected Point lp2dp (Point lp)
	{
		int viewWidth = getWidth ();
		int viewHeight = getHeight ();
		
		Point dp = new Point ();
		
		dp.x = ( mImageLeft + lp.x * mScale ) + viewWidth / 2;
		dp.y = ( mImageTop  + lp.y * mScale ) + viewHeight / 2;
		
		return dp;
	}
	
	protected void _2dp (Point lp)
	{
		Point dp = lp2dp (lp);
		lp.setLocation (dp);
	}
	
	protected Point dp2lp (Point dp)
	{
		int viewWidth = getWidth ();
		int viewHeight = getHeight ();

		Point lp = new Point ();
		
		lp.x = ( dp.x - viewWidth  / 2 - mImageLeft ) / mScale;
		lp.y = ( dp.y - viewHeight / 2 - mImageTop  ) / mScale;
		
		return lp;
	}
	
	protected void _2lp (Point dp)
	{
		Point lp = dp2lp (dp);
		dp.setLocation (lp);
	}

	protected Dimension ds2ls (Dimension size)
	{
		return new Dimension (size.width / mScale, size.height / mScale);
	}
	
	protected void _2ls (Dimension size)
	{
		Dimension ls = ds2ls (size);
		size.setSize(ls);
	}
	protected Dimension ls2ds (Dimension size)
	{
		return new Dimension (size.width * mScale, size.height * mScale);
	}
	
	protected void _2ds (Dimension size)
	{
		Dimension ds = ls2ds (size);
		size.setSize(ds);
	}
	
	// PropertyManagerListener
	@Override 
	public void notifyEntityChanged (EntityChangedEvent event)
	{ 
		repaint ();
	}
	
	// AncestorListener
	@Override
	public void ancestorAdded(AncestorEvent event) 
	{
		
		if ( mTiled2dBackground != null )
			mTiled2dBackground.addEntityObjectListener (this);

		
		SharedData.sSelectedModuleRef = null;
		SharedData.sTiled2dBackgroundView = this;
	}

	@Override
	public void ancestorRemoved(AncestorEvent event)  
	{
		if ( mTiled2dBackground != null )
			mTiled2dBackground.removeEntityObjectListener (this);
		
		SharedData.sSelectedModuleRef = null;
		SharedData.sTiled2dBackgroundView = null;
	}
	
	@Override
	public void ancestorMoved(AncestorEvent event) 
	{
		
	}
	
	
	boolean mMovingImage = false;	
	int mLastImageX;
	int mLastImageY;
	Point mRightButtonStartPoint = new Point ();

	void startMovingImage (Point startDevice)
	{
		mMovingImage = true;
		mRightButtonStartPoint.setLocation(startDevice);
		
		mLastImageX = mImageLeft;
		mLastImageY = mImageTop;
	}
	
	void updateMovingImage (Point point)
	{
		  updateSomeValues (mScale, 
				  mLastImageX + point.x - mRightButtonStartPoint.x - mImageLeft,
				  mLastImageY + point.y - mRightButtonStartPoint.y - mImageTop
				  );
		  
			
		  setCursor (Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		  
		  repaint ();
	}
	
	void endMovingImage ()
	{
		setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		
		mMovingImage = false;
	}

//--------------------------------------------------------------------------
// as a MouseListener
//--------------------------------------------------------------------------

	
	boolean mSelectingRegion;
	//Point mLeftButtonStartPoint = new Point ();
	int mStartCellCol;
	int mStartCellRow;
	
	public void startRegionSelecting (Point dp)
	{
		mSelectingRegion = true;
		
		Point lp = dp2lp (dp);
		mStartCellRow = lp.y / mTiled2dBackground.getCellHeight();
		mStartCellCol = lp.x / mTiled2dBackground.getCellWidth ();
		
		mTiled2dGridSelectedRegion.setRegion(mStartCellRow, mStartCellCol, mStartCellRow, mStartCellCol);
		mTiled2dGridSelectedRegion.makeRegionValid (mTiled2dBackground);
	}
	
	public void updateRegionSelcecting (Point dp)
	{
		Point lp = dp2lp (dp);

		mTiled2dGridSelectedRegion.setRegion(mStartCellRow, mStartCellCol, 
				lp.y / mTiled2dBackground.getCellHeight(), 
				lp.x / mTiled2dBackground.getCellWidth ());
		mTiled2dGridSelectedRegion.makeRegionValid (mTiled2dBackground);
		
		updateGridSelectionRect ();
		
		repaint ();
	}
	
	public void endRegionSelecting ()
	{
		mSelectingRegion = false;
	}
	
	public static void toggleTiledBackgroundLayerVisibility (Tiled2dBackground background, int layerID)
	{
		if (background == null)
			return;
		
		if ( layerID < 0 || layerID >= background.getLayersCount() )
			return;
		
		Tiled2dLayer layer = background.getLayerByIndex(layerID);
		Tiled2dLayerStatus layerStatus =  Tiled2dLayerListCellRenderer.getTiled2dLayerStatus (layer);
		layerStatus.setVisible( ! layerStatus.isVisible() );

		// wired ?
		// just to raise property changed events, then update some UIs
		layer.setName(layer.getName());
		layer.commitModifications();		
	}

	public static void toggleTiledBackgroundLayerLockedStatus (Tiled2dBackground background, int layerID)
	{
		if (background == null)
			return;
		
		if ( layerID < 0 || layerID >= background.getLayersCount() )
			return;
		
		Tiled2dLayer layer = background.getLayerByIndex(layerID);
		Tiled2dLayerStatus layerStatus =  Tiled2dLayerListCellRenderer.getTiled2dLayerStatus (layer);
		layerStatus.setLocked( ! layerStatus.isLocked() );

		// wired ?
		// just to raise property changed events, then update some UIs
		layer.setName(layer.getName());
		layer.commitModifications();		
	}

	void toggleLayerVisibility (int layerID)
	{
		toggleTiledBackgroundLayerVisibility (mTiled2dBackground, layerID);
	}
	void toggleLayerLockedStatus (int layerID)
	{
		toggleTiledBackgroundLayerLockedStatus (mTiled2dBackground, layerID);
	}
	
	// Method descriptor #12 (Ljava/awt/event/MouseEvent;)V
	public void mouseClicked(java.awt.event.MouseEvent e)
	{
		if (e.getSource() instanceof JList)
		{
			return;
		}
		
		if (e.getButton () == e.BUTTON3)
		{
			if ( mCopySourceBackground == null )
			{	
				if ( mMouseInGridSelectionRect && SharedData.sTiled2dBackgroundView != null ) // it must be this
				{
					SharedData.sTiled2dBackgroundView.setCopySourceBackground (
							mTiled2dGridSelectedRegion.createTiled2dBackgroundFromGridSelectionRegion(mTiled2dBackground) );
				
					repaint ();
				}
			}
			else		
				setCopySourceBackground(null);
		}
	}
	  
	// Method descriptor #12 (Ljava/awt/event/MouseEvent;)V
	public void mousePressed(java.awt.event.MouseEvent e)
	{
		if (e.getSource() instanceof JList)
		{
			return;
		}
		
		requestFocusInWindow(true);

		mLastMousePos.setLocation(e.getPoint());
		
		//PureModule module = getModuleAtPoint ( e.getPoint() );
		
		
		if (e.getButton () == e.BUTTON1)
		{
			if ( mCopySourceBackground != null )
			{
				
			}
			else
			{
				startRegionSelecting (e.getPoint());
			}
		}
		  
		if (e.getButton () == e.BUTTON3)
		{			
			startMovingImage (e.getPoint());
		}
	}
	  
	// Method descriptor #12 (Ljava/awt/event/MouseEvent;)V
	public void mouseReleased(java.awt.event.MouseEvent e)
	{
		if (e.getSource() instanceof JList)
		{
			if (e.getButton () != e.BUTTON3)
				return;
				
			JList list = (JList) e.getSource();
			int layerID = list.locationToIndex (e.getPoint());
			
			//toggleLayerVisibility (layerID);
			toggleLayerLockedStatus (layerID);
			
			return;
		}
		
		
		mLastMousePos.setLocation(e.getPoint());
		

		
		if (e.getButton () == e.BUTTON1)
		{
			if ( mCopySourceBackground != null )
			{
				Point lp = dp2lp (mLastMousePos);
				int row = lp.y / mTiled2dBackground.getCellHeight();
				int col = lp.x / mTiled2dBackground.getCellWidth();
				
				mTiled2dGridSelectedRegion.copyTiled2dBackground(mCopySourceBackground, mTiled2dBackground, row, col);
				
				return;
			}

			
			if (mSelectingRegion)
			{
				updateRegionSelcecting (e.getPoint());			
				endRegionSelecting ();
			}
		}
		  
		if (e.getButton () == e.BUTTON3)
		{			
			if (mMovingImage)
			{
				updateMovingImage (e.getPoint());
				endMovingImage ();
			}
		}  
	}
	  
	// Method descriptor #12 (Ljava/awt/event/MouseEvent;)V
	public void mouseEntered(java.awt.event.MouseEvent e)
	{
		mLastMousePos.setLocation(e.getPoint());
	}
	  
	// Method descriptor #12 (Ljava/awt/event/MouseEvent;)V
	public void mouseExited(java.awt.event.MouseEvent e)
	{
		
	}
	  
//--------------------------------------------------------------------------
// as a MouseMotionListener
//--------------------------------------------------------------------------

	// Method descriptor #9 (Ljava/awt/event/MouseEvent;)V
	public void mouseDragged(java.awt.event.MouseEvent e)
	{
		mLastMousePos.setLocation(e.getPoint());
		if ( mCopySourceBackground != null )
			repaint ();
		
		if (mMovingImage)
			updateMovingImage (e.getPoint());
		
		if (mSelectingRegion)
			updateRegionSelcecting (e.getPoint());
	}
	  
	  // Method descriptor #9 (Ljava/awt/event/MouseEvent;)V
	public void mouseMoved(java.awt.event.MouseEvent e)
	{
		mLastMousePos.setLocation(e.getPoint());
		//if ( mCopySourceBackground != null )
			repaint ();
	}
	  
//--------------------------------------------------------------------------
// as a MouseWheelListener
//--------------------------------------------------------------------------

	// Method descriptor #8 (Ljava/awt/event/MouseWheelEvent;)V
	public void mouseWheelMoved(java.awt.event.MouseWheelEvent event)
	{
		float scale = (float)mScale + ( event.getWheelRotation () > 0 ? 1 : -1 );
		
		if ( scale < MinScale)
			scale = MinScale;
		if (scale > MaxScale)
			scale = MaxScale;

		updateSomeValues ((int)scale, 0, 0);
		repaint ();
	}
	  
	
//--------------------------------------------------------------------------
// as a KeyListener
//--------------------------------------------------------------------------

	  // Method descriptor #10 (Ljava/awt/event/KeyEvent;)V
	  public void keyTyped(java.awt.event.KeyEvent e)
	  {
		  
	  }
	  
	  // Method descriptor #10 (Ljava/awt/event/KeyEvent;)V
	  public void keyPressed(java.awt.event.KeyEvent e)
	  {
		  switch (e.getKeyCode())
		  {
		  case java.awt.event.KeyEvent.VK_1:
		  case java.awt.event.KeyEvent.VK_2:
		  case java.awt.event.KeyEvent.VK_3:
		  case java.awt.event.KeyEvent.VK_4:
		  case java.awt.event.KeyEvent.VK_5:
		  case java.awt.event.KeyEvent.VK_6:
		  case java.awt.event.KeyEvent.VK_7:
		  case java.awt.event.KeyEvent.VK_8:
		  case java.awt.event.KeyEvent.VK_9:
		  {
			  int layerID = e.getKeyChar() - '1';
			  toggleLayerVisibility (layerID);
			  break;
		  }
		  case java.awt.event.KeyEvent.VK_W:
		  {
			  mPaintGrid = ! mPaintGrid;
			  repaint ();
			  break;
		  }
		  case java.awt.event.KeyEvent.VK_X:
		  {
			  if (mCopySourceBackground != null)
			  {
				  Tiled2dGridSelectedRegion.xFlipTiled2dBackgroundWholly(mCopySourceBackground);
				  repaint ();
			  }
			  else
			  {
				  mTiled2dGridSelectedRegion.xFlipTiled2dBackground(mTiled2dBackground);
				  repaint ();
			  }
			  
			  break;
		  }
		  case java.awt.event.KeyEvent.VK_Y:
		  {
			  if (mCopySourceBackground != null)
			  {
				  Tiled2dGridSelectedRegion.yFlipTiled2dBackgroundWholly(mCopySourceBackground);
				  repaint ();
			  }
			  else
			  {
				  mTiled2dGridSelectedRegion.yFlipTiled2dBackground(mTiled2dBackground);
				  repaint ();
			  }
			  
			  break;
		  }
		  case java.awt.event.KeyEvent.VK_ESCAPE:
		  {
			  if (mCopySourceBackground != null)
			  {
				  setCopySourceBackground (null);
				  repaint ();
			  }
			  break;
		  }
		  case java.awt.event.KeyEvent.VK_DELETE:
		  {

			  mTiled2dGridSelectedRegion.clearTiled2dBackground(mTiled2dBackground);
			  repaint ();

			  
			  break;
		  }
		  case java.awt.event.KeyEvent.VK_I:
		  {
			  mPaintCellInfo = ! mPaintCellInfo;
			  repaint ();
			  break;
		  }
		  default:
		  }
	  }
	  
	  // Method descriptor #10 (Ljava/awt/event/KeyEvent;)V
	  public void keyReleased(java.awt.event.KeyEvent e)
	  {
		  
	  }

//--------------------------------------------------------------------------
// as a FocusListener
//--------------------------------------------------------------------------

	  
	  // Method descriptor #9 (Ljava/awt/event/FocusEvent;)V
	  public void focusGained(java.awt.event.FocusEvent e)
	  {
		  return;
	  }
	  
	  // Method descriptor #9 (Ljava/awt/event/FocusEvent;)V
	  public void focusLost(java.awt.event.FocusEvent e)
	  {
		  return;
	  }

}

